package com.cdtye.shuoh.controller.tdengine;

import com.cdtye.common.webcore.web.AjaxJson;
import com.cdtye.shuoh.config.fastdfs.FastDfsUtils;
import com.cdtye.shuoh.domain.tdengine.SelectDto;
import com.cdtye.shuoh.domain.tdengine.SelectVisualDto;
import com.cdtye.shuoh.service.tgengine.TdEngineService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;


@RestController
@RequestMapping("/dataOperation")
@Slf4j
public class TdEngineController {

    @Autowired
    private TdEngineService tdEngineService;

    @Autowired
    private FastDfsUtils fastDfsUtils;

    private static final Logger LOGGER = LoggerFactory.getLogger(TdEngineController.class);

    /**
     * 创建数据库
     * @param dataBaseName
     * @return
     */
    @PostMapping("/createDb")
    public AjaxJson<?> createDataBase(@RequestBody() String dataBaseName) {
        try {
            //调用创建数据库方法
            this.tdEngineService.createDateBase(dataBaseName);
            log.info("successful operation: created database '" + dataBaseName + "' success");
            return AjaxJson.success();
        }catch (UncategorizedSQLException e) {
            String message = e.getCause().getMessage();
            try {
                message = message.substring(message.lastIndexOf("invalid operation"));
            } catch (Exception ex) {}
            log.error(message);
            return AjaxJson.error(message);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxJson.error(e.getMessage());
        }
    }


    /**
     * @param selectVisualDto
     * @return R<?>
     * @MethodDescription 可视化数据  返回格式，时间，数值列
     * 历史数据 SELECT voltage ,ts FROM test.meters where ts between '2017-07-14 02:40:00.000' and '2017-07-14 02:40:00.001' LIMIT [topnums]
     * ts > now - 24h;
     * 实时数据 select col from table LIMIT [topnums]
     * 聚合数据 select [avg/max/sum/count..](col) from table where ts between '2017-07-14 02:40:00.000' and '2017-07-14 02:40:00.001' group by col LIMIT [topnums]
     */
    @PostMapping("/getVisualizeData")
    public AjaxJson getVisualizeData(@Validated @RequestBody SelectVisualDto selectVisualDto) {
        try {
            if (selectVisualDto.getType() == 0) {//查询历史
                return AjaxJson.success(this.tdEngineService.getHistoryData(selectVisualDto));
            }else if(selectVisualDto.getType() == 1) {//查询实时
                return AjaxJson.success(this.tdEngineService.getRealtimeData(selectVisualDto));
            }else if(selectVisualDto.getType() == 2){//查询聚合
                return AjaxJson.success(this.tdEngineService.getAggregateData(selectVisualDto));
            }else { //查聚合函数（max,min,avg汇总）
                return AjaxJson.success(this.tdEngineService.getAllAggregateData(selectVisualDto));

            }
        } catch (UncategorizedSQLException e) {
            String message = e.getCause().getMessage();
            try {
                message = message.substring(message.lastIndexOf("invalid operation"));
            } catch (Exception ex) {
            }
            log.error(message);
            return AjaxJson.error(message);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxJson.error(e.getMessage());
        }
    }

    /**
     * 获取最新数据
     * @param selectDto
     * @return
     */
    @PostMapping("/getLastData")
    public AjaxJson<?> getLastData(@Validated @RequestBody SelectDto selectDto) {
        try {
            return AjaxJson.success(this.tdEngineService.getLastData(selectDto));
        }catch (UncategorizedSQLException e) {
            String message = e.getCause().getMessage();
            try {
                message = message.substring(message.lastIndexOf("invalid operation"));
            } catch (Exception ex) {}
            log.error(message);
            return AjaxJson.error(message);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxJson.error(e.getMessage());
        }
    }

    /**
     * 获取最新数据
     * @param selectDto
     * @return
     */
    @PostMapping("/getLastNumData")
    public AjaxJson<?> getLastNumData(@Validated @RequestBody SelectDto selectDto) {
        try {
            return AjaxJson.success(this.tdEngineService.getLastNumData(selectDto));
        }catch (UncategorizedSQLException e) {
            String message = e.getCause().getMessage();
            try {
                message = message.substring(message.lastIndexOf("invalid operation"));
            } catch (Exception ex) {}
            log.error(message);
            return AjaxJson.error(message);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxJson.error(e.getMessage());
        }
    }



    @GetMapping("/saveImage")
    public AjaxJson<?> saveImage() {
        String path = null;
         String str = "FFD8FFE000104A46494600010200000100010000FFE100E645786966000049492A00080000000500120103000100000001000000310102001C0000004A00000032010200140000006600000013020300010000000100000069870400010000007A000000000000004143442053797374656D73204469676974616C20496D6167696E6700323030393A30393A32332031343A33323A303100050000900700040000003032323090920200040000003334330002A00400010000008000000003A0040001000000A400000005A0040001000000BC0000000000000002000100020004000000523938000200070004000000303130300000000000000000FFC000110800A4008003012200021101031101FFDB00840007040506050407060506070707080A110B0A09090A150F100C1119161A1A181618181C1F28221C1D261E1818232F2326292A2D2D2D1B213134312B34282C2D2B010B0B0B0F0D0F1E11111E402B242B4040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040FFC4009800000203010101010000000000000000000506030407020108001000020103020404030506040505000000010203000411052106123141132251610771A114328191B11523334262C1165272D124343573F025435392B2010002030101000000000000000000000002030104050006110003000202010303030500000000000000010203112131041222411432510533611323428191FFDA000C03010002110311003F00DBB56815D25046F83B8A4C867B9B6BE8ED7ED4D22B86FBFB9C8A7AD4D70928F6359C3B91C456C03AB798F43ED44919F97499666B963AB411BAEEC1BCC3A74A289696CF6D25CCF0A4AC80901867A50DBA41FB46193D33456CBF7BA64CBEA187D29564E25CB2B58F13E9CB182F6696E7B9F0F6FCEAD5C7115BDE6993A5A4F0AB00393958024F6A53D4229F4FB5090B06B89815851BAB1C509E13BBB5BCD4C5BC5CCF36099A7032BE267EE83E8282D6B84463F21BE1A1A46BCB69AC5D8E21B59E42101F0ADBF9180FBC3D4114E7C276D6918FDA76D78A6DEE6218562032E77DCF4AC8753E30974BE2113C6915F3468611CDF74EFD49EE05286B5C7DC41AACD2FD9357B8F00B6522B68C2A27A0500741EB418FD53BD966276F7A3E96D765824102C12A49CB90795B9B1D3AD504C6F5F3C68FAC71C58235DD93EA37509FBC590B29237E86B50F86FF001017889CDA6A08B6F780631D398F718EC7DA9D369F02F361B4FD4D0F07D866B96D90935F89C66B8E6F2134C2B6CE798649F5150B3F9589DABB90E14E3B0AAF210B1316E98A8276433919F7AA450B499DF7AB6EA0EF50A802439000380093D6B881DB585C2CBEEA4FD2B3392DC26B966C411FBCFEC6B50D780168CF8FE522B3CB942FAADA80700499FA5148ECF3C9D5E8FDEA9F4CD17D0543E972161B731A17A80C6FF3A33C2EBFFA448F8C80C6957D858C48D7ECAFAF2DB52D764596D85844F158C43A91D19CFCFB7B52B71F717DBF0CC36BA0E92896CA90A35C188798B919209AD844AB2A3C6E399641C8C0FA1AF967E262C91FC40D5A09723C3BA618CFE55CE40C329D6C2BC3904DADCAF25D64C65F0403B7CBE55AFF0007697631C691ADBC48A07F96B38E0F516DA4A3003CE398D685C2FAA490C0CD6FA6DF5E329DCC6A303F3ACFC8DDD1BF865441A558585B32050AB803B52C7C48E0B88C6BAFE87108B50B45E69047B78F18EA0FF50EA0FB63BD19D278822BAB30E96D245328F344C30C3E7566D38864BF964B6FD972A2A0DE4F1548F7DBAFD29B2E742AD537FC003837591AEE8515E672C728E7A02477A2ACCB82BCDB9A5DE06B14D374FD42D62528897F3051ED9A3DB0031BED5765EE5330B22536D23876DB19C55795811CBD6A4B8603D3AD449FC5391D2880D9CB0EB8C673DEB9E557E5E619C1CEFEB523E7036DF3512B0C9DF7077AE250EFC42B9D227C750BB566E9273EB76CAC46412703E55A3F10385D3CE580CB01BD672D094E2F880FBBE1B30353237C9AF7A44DA9ED8F7A2FC3F22A68B32648EA7E941F57C8553FD557F43653A6CC1BDCFD2957D871D338B3BAE794073851B1C562BF1C785E697E21DC5ED8BC456F2249CC7CDE607183B7E15A534B3F393102581CE3DA877C44D0EF2E8596B10A3340D12248C01D8A67CA4F6CE45766A733B44FE9E9647EE13F82AD1EE6C2DA199BC372314FBA1F09343A9C77B737724CB1AB0111384C30C7DD1E9D41EB9A4481A5B7BA5683F94ED4FDC31C40D2C3C9291CCBB6F59ADFBB66ECAF6E864E15BC897895AD72CC258FC305DB2C703A9F738A23A7702D8D96B17576AD287B9757F1031CA95391CA7B7BFA8A47D16CF56B9E2386E2DAF228A2495A46648F2C47CFDAB5AB433436205D5C78F28C92FC9CB919DB6F953B0A4D722B3372F862DCF609A7DC5DC319C8795A5FFEC735131F310770455BD5A40F74C4105B97723D6A8C870A77DF157A3ED46066FDC643385C6C4004FAD719C7310715FA21E5507E66B87602238F5FCE8C52FC923963CA176EF559DD50C873803A9A96277F014BE01C6F504E1790A9C1C8DEA09DED0EBC55295B68E318F3B1CE7A74A598EDD25BD17270D22A103D851FE2F259618C360B676CE3341B440A6EA540413E190715DBD4EC666F7791A03EB5B2A7FAAACE93FF00232EF8EBFA557D7C72F229CECF8AF6CDC25B1C139C1CE6936FDC3E3A671C3F0ADCCEDE26F938CD687A4D8C50E92B6DC80C6EA4329EE0F6A42E1B6412E548EA4D2EFC72F891736CA9A070C5E490CB072B5DDDDBC982186E23523F33F97AD37B64E0D4CA16B8A208F4BE2DBEB18F091C770E899EC33B571A45F7FC5BAC84A11D0AEFDABAE1A92D7E224D224D722D38818733C7337EEEE8F7643D55BD46FEDB57979C33AB6877E12FAD2785B3E4917757F91E86B3726372DECD8C39154AD05F866C6E9B500D2BEB32AAB650DBB85C7D2B4FD3965B1F3497576F1C83222B920F87F2207D291B8224D78DDF836D0AC83B993C9C83D69B6F2E279EE0A5C382D19E5DBA67B9A3C30EF803CBF25629E51EDCBF3485FB9C9A8659179826FD0D7858E0737CAB8998729619E9BD6825A3CEB6DF270F2049554024B637AF032AFDFC0EB5C4ADCAA8483CDEA4D448DCC599FA63BD483B26770EADE6C60F6AAF73272863ED5E22E432AEFE6AAD76D96600F41D2BB44FAB8D8E7C628B25DC7CCD8089D3D77A1BA02A8BF9191B2A54A8DB1B55DE30915751031BF20A5F6D66DF428AE752D45C082DEDDE46C75DBB0F72703F1A87F61D553F55FECFDC653456644D7122C5106C966380292753E3EB78E17874A84CCE76F164F2A8F90EA7E9599717718EB1C51AA3DF5F4EEA81BF776E8C4242BD801FDFA9A9341BE372C63B8DC8E8F5138D7F917DAEC3777A9EA9708C8F793AC6DD5118A83F9507861C5D98DD82A483CA49C6187FB8FD28F78202F230DEA09AD11D4F32823B834ED1096817F649ACAE567819E396360C8CA7041EC41AFA03E1371D43C59A78D1F5F489B5144EAEA396E5477C7F98771F8D62163670967313B72A372140C701BAF4ED442D0C967324F6F2345344C1D1D0E0A91DE85CA61CD3966F97DA7E9DA66AD1496D28849520C65C6327A0DF7DFF002F7A54E26E2886D38C23B3BA85AD96EEDD25859F00AB64AB2B7E2BD69678BB57FDA1A0CBC5105D78579246B6F796EAD806451E56F65DB38C819EBD28D71C7088E37E17D375AB1BEE5BDB2B500B2A901B9C06391E99CFF6AAF09437C0DCDFDE9D361991C9EA722A2E7519C6727A8A46E0EE2AB99ADA4B2BBC34D68FE14809F30C1C7F6A675D42DEE1B08EBCC173CBD0E2AC25F264E49A8EC9CCAED31058951B835CC527EF002C4A9DC9355EC98C911661B8CE0E6A0793C2BA4C92C98E52476352D15BD7A49971E5C3011B6096CE477AAC64324859B6CE466BA4CA464BFF009BAE2A3322A3904021739CF5151A0D56FB1F388D564D5642E83081483EBEA2B20F8F576B69C2A91C4581BAB955619EC016FEC2B5AE2A0CDA84BB9F260803EB58D7C7E4E6D234E2C3F762E5C648E995DBF4342BA1B493F274D7C9956993453C8D1310ACEB819F5A216024B4DCA6F9DE827D8642D98F6C77CE299743B9568152F591A4071907A8A94693186CEE96EEC0329CB45BFE1DEA5B6903E7355B4D863827E688828DD45578E5FB3DF4B031DD1C8F98EDF4A3D8059BEB59E1985EE9E01940C491138132FA7B1F4356ED2E62BFB6F1602460E19186190F7047635EC12865DEABDCDB32DC7DAEC4AA4F8C3A9385947A1F43E87FB549C59851668DEDA648D95882BE2AF32AB020838FFCEF5B5F02DD5B4561269926A2B3C7BC510480AA2A9F3020E361962304ED8AC56D654B91CEA1A39176646D994FBD687F0FB5E72935BDE5EDC45E1471F2189398955246FB1E81BF414BA9F90E2B4F4236AF60FA0FC56BC81D4C6B7A0B15ED9E87EA01FC6AC6BB72F6308BE43E6B4955C9FE824061F2C1FA0A2FF19184DC47A3EA4165F1032C7234B1F216392A4E3E9F950DD6E0173637507FF340CBF89538FAD4E2FB7446449B19D24478A2280989C676EF5D327EF0F205080E7F1A09C217CD71C35692B125846011FAD106983A141E52493B9A231AD29A72599A5279438E65CE46F8AAB712FEE58730C9DCE0D70D3E2206462DCA411F9509D5358B5B2B80B3C87C4239BA676F7A17A5D9CB75C4F267127C40E2DBA766975DBF677EA4B75FA509E27E20D6F568123D56FAE6E903730121D81031B7E75BFF00195A685A1DDDEC96FA45AC912382CA8807203B647B67F5A4DF8A10E9127054F710DB40AF132189A2C655890307E609AAB8D3DF66EE6F26384B1F6F5B320D3B53FB3868AEA3F16072323B8A330DA4322ACF60FE2427D0EEBF3A0C96F1CEB91E53E953DA5B5DD84C25B42467A8EA187BD5844319F4DB99AD27549F2636FBAD52F10BF87AA4130D84D10DFDC6DFED5259A35F6909218BC3719F21EDEE3DAAB6B5CD26970B9FBD6F2807E4C31FA81440056C26E78C55B0F8A07A54C428A2B15CA31C31A24C8D135E2CE2217562A1E78BAC676F117BAFFB7BD5ED0B5804457D63232F638D88EC548FA11552093948C554D4E36D3A59354B352D1B6F770AFF0030FF0038FEA1DFD45733865F8B9AA477575A6886E2FE61E3E55EE93008628D907037C9DFF023AD71A94E55A308096C03B526715EA5F6CB8D202DC4F323CA8D6E4383185D81F7CECB8A6BB96592E473300A1794FEB438935BD8591A7CA27E159D608E4B4CFF000E5C72FA6F9FEF4566917C466E519C9237EB48D6DAA1B4E29B9588F3A722391DC8231FDA8CDD6A66EE40A9910B0040407C4907F6144DE8CCF2F0D3AF525C04A7D5D4ABAC11BCCE9F7882028F627A52A6B904FAF7DA3C070677531469065C60292C4B631B7CE8BCDE0A5B37DBB92187B419D87CFD6A71A9C7170EDC5CC0D172476F394E4181E621063F0069174B5C85E343549CFF00D0CBEA12CFA35F5AEA0B6EECD280248F23C8410C0FD0FCEB2DE2C8A5FD98EF71130026508E4100F638F5E829DF599AEECF4094222BC524A8656E40595B7C00DDB3BFE54ADC631C5268D7E610DCCACB2B024E010403FAD6761A6A96CF4D971AF4BD0A30A67000DCFA53059DB7D96D43CB8E6C64F31C0141B479E148FC593CCEA7016AF33B5CC8971A89C40A72211DEB55194C256F7134803DB19182FF00EE63953F33D688DF409A869921B4789E668FF791A1EA46F95A5BD434B8755E67B1D56677FE58E7C81F2D860502827BDD36F0C2F23C6EA7A86FA8345BD1DAD8D7A7B148C1F6AB3704B46B340724300E9515845E2DBA306CE466AC1B5E4CB0247AE2B8E3CB4BF78DB924EC7AD1AB2B849414241076C5066B61CD939F9D58B25292794F7AE440BD756CDA7717D9D803FF00082633C4A7F93AE40F6EF4E534E923B329073D296B55B4B89F8C6DA48D805584B16C06E4C9E52403B1228DDA44B1808E1C2E3032726A67864B046AD8B3E2A8DD17995ADC0739C617277A316BAA4B1DC460AC515808B25F9B2C1BB03E94BFC777A6CB50B6F05989309C00301B07B9F419E955F86B5A79E16B59EFA481A43E590229E53E9D36150FB681B8573A61BE26D66195B9ADAE2391394E406CE0D45753183E1F95E62331A215FF00512685EBFA3DCDADBB48892DC073E5B8E6E627D548C6DEA3AF7AEB599CFF00850420EFE2C608F4C2553C90D5ED8EC53338E617C1AD48D626CAE74D79F0647562850EECA4E37FC684F12DBD945C37A97EE91DE5B69101271962BB7D7142755BE95788A51E25C85058AAAA2F2F4DB7CFAD54BB8DEEAD1E39F52BBE575C3031291FAD22561E1B2C5FD4BEBA335D2E48D2FE1FB4372C45C739F414C977A9C018FD963121ECEFD3F014AD79035B5DCB0BEED1B95CFAFBD4B6D279D06700B0157E5959C8C96524B3B335DBF908F2A85C807E5919A250C3A65E6A36B677D6A6E98A305690F2B228DF60B80067A0AA77056C61C900B01E55AA9A34F2C3AA0BF95F0C3619F4A66C00D59DB2DB5C18AD99CC41CAF2B64F28CF5CF7144DA060CC8EA430D88AF34A9A1322F2801BAF5A878B62F134D6BEB291A0BEB25F123656DA451B9561DC54904530911548F4AE61B8E57F30C63B5737B7B3C36F69A98412E9D788AECBD1A063D77EE3E757E6D29648D67818BA30C8E5AE248D0DB1D4A197C503C68CC2C0F50DF7948FC88FCABABCB4BD8E4F16325D7D3A8A15AA69F24A628EC15CDEAC8AF0A8072CC0EC31EF5B8FC3FE12B04D0A393896D2D8DD93CE63123F941ECC41C1C505E598EC6462ABE51F3C7C43695E3B1B88F208E78DB1D8EC68068ED3ADF42D3A931971CCA46322BEBD8F82B82B525F15B43B19A2826DC966762E074C67F5A8B51F87BC09A95C81370EDA5B731E6F12DCB46C081FD271497E44EF63960AD68C5A08A5FB1BC16E5482BE50FBEE3A5237104B721274B85113A4A15906C73835B7F197017EC985B53E12B89352D3D54992DE470648D874E56C7981DC6FDF1BEFB63DC71736BA958C1A95848248E5215F0305580E8C3B1C7E94557392772296378EBDC307146A715A5FDDCD2B8C5BF2860A32467A505B3D6DF5263E1C4047D0731DCD5AE28D2A4BBD5788163C9334917281EC4545A070E5D5A60C85718E98AA5E85E9FE4B8B2FE4A1ABE833EA37113D98413BE13909C07F4DFD697AE6DA6B49DEDEEA268A54382ADD456A767A785B981D9BF872AB2E074DC543C6FA2D9DFDB5CCCF1E25B40EF91D5941DC7CF1923E54CC5929714272FA5BDC99E41A9CB855B86E703604D5DBEB80D6B1BC441008CE2A2BDE1D956113D8CBE344E39973DFF1A11F6896D1CC53AB27F49AB6995F87D0D1A3EAA20D5E2F1490B31E5033D288EB5766EA27B280E6420895CF48E926F271288E48DBEE91D0F4A357D7F21871CE1A49002CE0633B512643434DB5E5A0D1EC6156E7B3702D5F3D8F407F31F5A2BA3AC96308B597CCA87CBF2A41D3A61270EDE59970268D84D18CF5DC74A79D2356B5D42C217670B38501D49DC1A34F60B5A1DBE1DC505D713C6B22AB62190A923254EDD0F6AD0A6B58D1482C07D2B36F8757305B712ACB348A10C322E49D812053F4D7D907965E719E837FCAB3BCBD7F50D1F13F6CF06A163A05AC8D0C05A4937C44B92C68AD9EB5A26956C162B98CC970BE348D21C364F63DF6FA52B5FEBB169C92DC3C66E30A79622843337A0AEECF59D334DD20EA5169F2CDA8DDA735DAF86B9B60077C9D947D6ABCD6BA2D5426B90DFC42B86D4BE166B5FE1E990DD7D82468446398E7192001DC8CE3DEBE348ADEF62B6B99A649D2272049CEA4066CE4139EFD7FF0D6BDC69F16748BBD2E4B3E18D324B7926D9EF1D8A155CEFCAA0F7F7DB7A43FDBECC98E7794E3CC59B20D5FC29B9F77067E6693F6F2697716F10D5E7217762D93EBB8A9BECD191D0F4AF2EBFEAD2FCDBF51530E9F855517470967111CC41CAE08DFDEABEBC078B7C0805489948EC4156CD5F8FEE37CAA86BDFC6BEF9CBFA35483F833FE0D90C9059DBB01E1C913061EA57383F3AEF5DD36D6EECE4F1A3F32825586C41150F047DFB0FF00B727F7A21A87FCA4BFE96FD2AE4F42AB8A333C9527048A9229E5E70BCE71511EA6BD87F8AB5238B1F69946086DF26A58AF270432B953EA0E2AAB74FC4D771D7101FD2B58D45C3C62F6740064156C1A6BD0F89B5D3A7CAE756BA262FBBBAFFB523E8DFC593FD34CBA1FFD2AEBE46A2A5576829A72B860EFF18F115FDC334FABDD024907C3217F415FAC78B756B6E13D4B458A75FB35DDD0799C8CC8DE5DC7375C1C0DA82E9FFC63FEA35E47FF002D71FF00787FF9340E6574864DD3DEDFC104CE40201DAAC427119C7B5569FBD598BF86698299FFD9";
         byte[] bytes = new byte[str.length() / 2];
         for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) (0xff & Integer.parseInt(str.substring(i * 2, i * 2 + 2).toUpperCase(), 16));
         }
         LOGGER.info(str);
         LOGGER.info("path"+path);
         path = fastDfsUtils.uploadFile(bytes,bytes.length, "jpg");
        return AjaxJson.success(path);
    }

}
